package com.wwj.study.rmi.rpc.client;

import com.wwj.study.rmi.rpc.RpcRequest;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.net.Socket;

public class RpcClientInvocationHandler implements InvocationHandler {
    private String hosts;
    private int port;

    public RpcClientInvocationHandler(String hosts, int port) {
        this.hosts = hosts;
        this.port = port;
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //执行方法先进去这里-代理对象方法

        RpcRequest rpcRequest=new RpcRequest();
        rpcRequest.setClassName(proxy.getClass().getName());
        rpcRequest.setMethodName(method.getName());
        rpcRequest.setParams(args);
        TCPTransport tcpTransport=new TCPTransport(hosts,port);
        return tcpTransport.send(rpcRequest);
    }

}
